define(function(require){
    //
    //var typeTime_1 = true, typeTime_2 = true, typeTime_3 = true, typeTime_4 = true;
    var tables = {};
    //include
    require('../mods/list-tpl');

    var listTpl = juicer.templates['list.juicer.html'];

    var List = {
        init : function() {
            this.bindEvent();
            this.initOrderTable("type_1_table",1);
            this.initOrderTable("type_2_table",2);
            this.initOrderTable("type_3_table",3);
            this.initOrderTable("type_4_table",4);
            this.initDatetimepicker();
        },

        initDatetimepicker : function() {
            $('.form_date').datetimepicker({
                language:  'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                minView: 2,
                forceParse: 0
            });
        },
        searchList : function(){
            $.each(tables, function (index, item) {
                $('#'+index).DataTable().ajax.reload();
            });

        },
        /**
         * 初始化订单的列表
         * @param tableId
         * @param orderType
         */
        initOrderTable : function(tableId,orderType) {
            var _table = $("#" + tableId).dataTable({
                "oLanguage": CRM.pageConfig,
                "sScrollX": "100%",   //表格的宽度
                "sScrollXInner": "100%",   //表格的内容宽度
                "bScrollCollapse": true,  //当显示的数据不足以支撑表格的默认的高度时，依然显示纵向的滚动条。(默认是false)
                "bPaginate": true,  //是否显示分页
                "bLengthChange": true,  //每页显示的记录数
                "bFilter": false, //搜索栏
                "bSort": false, //是否支持排序功能
                "bInfo": false, //显示表格信息
                "bAutoWidth": false,  //自适应宽度
                "bServerSide": true,//开启服务端处理
                "sAjaxSource": CRM.getOuterUrl('/trade/crm/order/queryPreSaleProblemOrder'),
                "fnInitComplete": function () {
                    this.fnAdjustColumnSizing(true);
                },
                "fnServerData": function (sSource, aoData, fnCallback) {
                    var dataString = {
                        type:orderType,
                        "operator": $.cookie('_uk'),
                        "pageNo": aoData[3].value / aoData[4].value + 1,
                        "pageSize": aoData[4].value
                    };
                    $.ajax({
                        "type": 'POST',
                        "url": sSource,
                        "dataType": "json",
                        contentType: "application/x-www-form-urlencoded",
                        xhrFields: {
                            withCredentials: true
                        },
                        "data": dataString,
                        "success": function (req) {
                            if(req.status == false && req.responseCode == 10212){
                                var loc = location.href;
                                location.href = CRM.getOuterUrl('/member/login.html?redirectUrl='+encodeURIComponent(loc));
                            }
                            else if (req.responseCode && req.status) {
                                var resultData = CRM.getDataTabelDtaObject();
                                if (req.entry) {
                                    resultData.iTotalDisplayRecords = 9999;
                                    resultData.iTotalRecords = 9999;
                                    for (var length in req.entry) {
                                        resultData.aaData.push([req.entry[length]]);
                                    }
                                }
                            } else {
                                $("#msg_dialog_content").text(req.message);
                                $('#msg_dialog').modal('show');
                            }
                            fnCallback(resultData);
                        },
                        error: function (e) {
                            //请求出错处理
                            $("#msg_dialog_content").text("订单列表,请求服务异常！");
                            $('#msg_dialog').modal('show');
                        }
                    });
                },
                "aoColumns": [
                    {
                        "bSortable": false,
                        "mRender": function (data, type, row) {


                            return renderOrderListMain(data);
                        }
                    }

                ], //列设置，表有几列，数组就有几项
                "bStateSave": true, //保存状态到cookie *************** 很重要 ， 当搜索的时候页面一刷新会导致搜索的消失。使用这个属性就可避免了
                "sPaginationType": "simple" //分页，一共两种样式，full_numbers和two_button(默认)

            });
            tables[tableId] = _table;
        },
        bindEvent : function() {
            var self = this;
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                tables[$(e.target).attr("aria-controls") + "_table"].fnAdjustColumnSizing();
            })
            $(document).on('click', '.J_groupid', function (ev) {
                //ev.preventdefault();
                var target = ev.target;
                var groupid = $(target).attr('data-groupid');
                $('#pintuan_iframe_content iframe').attr("src","http://www.52shangou.com/buyer/pintuan/group-detail.html?groupId="+groupid);
                $('#pintuan_iframe').modal("show");
            });
        }
    };

    /**
     * 渲染订单列表主信息
     * @param data
     */
    function renderOrderListMain(data) {
        if(data){
            data.remarkUrl = CRM.getOuterUrl('/buyer/crm/php/o2o/seller/admin/order_remark.php');
            var html = listTpl({data : data});
            return html;
        }

    }

    window.doSearch = List.searchList;
    return List;
});